package com.qst.express.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.commonUtils.express.RandomUtil;
import com.qst.express.entity.Express;
import com.qst.express.entity.User;
import com.qst.express.entity.vo.ExpressVo;
import com.qst.express.mapper.ExpressMapper;
import com.qst.express.service.ExpressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.express.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author mjh
 * @since 2022-09-25
 */
@Service
public class ExpressServiceImpl extends ServiceImpl<ExpressMapper, Express> implements ExpressService {
    @Autowired
    private UserService userService;
    @Override
    public IPage<Express> pageTeacherCondition(Page<Express> expressPage, ExpressVo expressVo) {
        QueryWrapper<Express> queryWrapper = new QueryWrapper<>();
        if (expressVo != null ) {
            String number = expressVo.getNumber();
            String username = expressVo.getUsername();
            String userphone = expressVo.getUserphone();
            String company = expressVo.getCompany();
            Integer status = expressVo.getStatus();
            String begin = expressVo.getBegin();
            String end = expressVo.getEnd();
            if (!StringUtils.isEmpty(number)) {
                queryWrapper.like("number", number);
            }
            if (!StringUtils.isEmpty(username)) {
                queryWrapper.like("username", username);
            }
            if (!StringUtils.isEmpty(userphone)) {
                queryWrapper.like("userphone", userphone);
            }
            if (!StringUtils.isEmpty(company)) {
                queryWrapper.like("company", company);
            }
            if (!StringUtils.isEmpty(company)) {
                queryWrapper.like("company", company);
            }
            if ( status != null ) {
                queryWrapper.eq("status", status);
            }
            if (!org.springframework.util.StringUtils.isEmpty(begin)){
                queryWrapper.ge("intime", begin);
            }
            if (!org.springframework.util.StringUtils.isEmpty(end)){
                queryWrapper.le("intime", end);
            }
        }
        IPage<Express> eduTeacherIPage = this.baseMapper.selectPage(expressPage, queryWrapper);
        return eduTeacherIPage;
    }

    @Override
    @Transactional
    public List<Map<String, Integer>> console() {
        ExpressVo expressVo = baseMapper.getExpressTotal();
        ArrayList<Map<String, Integer>> data = new ArrayList<>();
        Map data1 = new HashMap();
        data1.put("express_size", expressVo.getExpressSize());
        data1.put("express_day", expressVo.getExpressDay());
        Map data2 = new HashMap();
        data2.put("outExpress_size", expressVo.getOutExpressSize());
        data2.put("outExpress_day", expressVo.getOutExpressDay());
        data.add(data1);
        data.add(data2);
        return data;
    }

    @Override
    @Transactional
    public List<Express> rankByMonth() {
        List<Express> data = baseMapper.rankByMonth();
        List<Express> data2 = new ArrayList<Express>();
        for (Express e : data) {
            Express express1 = new Express();
            String userphone =e.getUserphone();
            String sum = e.getSum();
            //获取姓名
            Express express2 = baseMapper.getName(userphone);
            express1.setSum(sum);
            express1.setUsername(express2.getUsername());
            express1.setUserphone(userphone);
            //获取图像
            User user = userService.getImg(userphone);
            if (user != null) {
                //express1.setImg(user.getImg()== null?"空":user.getImg());
                if(user.getImg()==null){
                    express1.setImg("0");
                }else{
                    express1.setImg(user.getImg());
                }
            }else {
                express1.setImg("0");
            }

            data2.add(express1);
        }
        return data2;
    }

    @Override
    @Transactional
    public List<Express> rankByYear() {
        List<Express> data = baseMapper.rankByYear();
        List<Express> result = new ArrayList<Express>();
        for (Express e : data) {
            Express express = new Express();
            String userphone =e.getUserphone();
            String sum = e.getSum();
            //获取姓名
            Express express2 = baseMapper.getName(userphone);
            express.setSum(sum);
            express.setUsername(express2.getUsername());
            express.setUserphone(userphone);

            //获取图像
            User user = userService.getImg(userphone);
            if (user != null) {
                //express1.setImg(user.getImg()== null?"空":user.getImg());
                if(user.getImg()==null){
                    express.setImg("0");
                }else{
                    express.setImg(user.getImg());
                }
            }else {
                express.setImg("0");
            }
            result.add(express);

        }
        return result;
    }

    @Override
    @Transactional
    public List<Express> rankByAll() {
        List<Express> data = baseMapper.rankByAll();
        List<Express> result = new ArrayList<Express>();
        for (Express e : data) {
            Express express = new Express();
            String userphone =e.getUserphone();
            String sum = e.getSum();
            //获取姓名
            Express express2 = baseMapper.getName(userphone);
            express.setSum(sum);
            express.setUsername(express2.getUsername());
            express.setUserphone(userphone);
            //获取图像
            User user = userService.getImg(userphone);
            if (user != null) {
                //express1.setImg(user.getImg()== null?"空":user.getImg());
                if(user.getImg()==null){
                    express.setImg("0");
                }else{
                    express.setImg(user.getImg());
                }
            }else {
                express.setImg("0");
            }
            result.add(express);
        }
        return result;
    }
    @Override
    @Transactional
    public List<Map<String, Integer>> consoleMonth() {
        List<Map<String, Integer>> data = baseMapper.consoleMonth();
        for (Map<String, Integer> datum : data) {
            System.out.println(datum);
        }
        return data;
    }



    // 根据用户手机号码查询所有快递
    @Override
    @Transactional
    public List<Express> findByUserPhone(String userphone) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userphone", userphone);
        List<Express> list = baseMapper.selectList(wrapper);
        return list;
    }

    @Override
    @Transactional
    public List<Express> findByUserPhoneAndStatus(String userPhone, int status) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userphone", userPhone);
        wrapper.eq("status", status);
        List<Express> list = baseMapper.selectList(wrapper);
        return list;
    }

    @Override
    @Transactional
    public boolean updateStatus(String code) {
        Express express = new Express();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("code", code);
        express.setCode("");
        express.setStatus(1);
        express.setOuttime(new Date());
        int update = baseMapper.update(express, wrapper);
        return update>0?true:false;
    }

    @Override
    @Transactional
    public Express findByCode(String code) {
        QueryWrapper<Express> wrapper = new QueryWrapper<Express>();
        wrapper.eq("code", code);
        Express express = baseMapper.selectOne(wrapper);
        return express;
    }

    @Override
    @Transactional
    public boolean saveExpress(Express express, String uphone) {

        express.setSysphone(uphone);
        express.setCode(RandomUtil.getCode());
        express.setIntime(new Date());
        express.setStatus(0);
        int insert = baseMapper.insert(express);
        return insert>0?true:false;
    }

    @Override
    @Transactional
    public boolean isExsitNumber(String number) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("number", number);
        Integer selectCount = baseMapper.selectCount(wrapper);
        return selectCount>0?true:false;
    }

    @Override
    @Transactional
    public boolean isExsitUserPhone(String uphone) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userphone", uphone);
        Integer selectCount = baseMapper.selectCount(wrapper);
        return selectCount>0?true:false;
    }

    @Override
    @Transactional
    public Express findByNumber(String number) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("number", number);
        Express express = baseMapper.selectOne(wrapper);
        return express;
    }


}
